package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.local.ACache;
import com.jzg.jcptadmin.data.vo.User;
import com.jzg.jcptadmin.viewinterface.MainActivityInterface;

import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2015/12/22.
 */
public class MainPresenter extends BasePresenter<MainActivityInterface> {
    private final DataManager mDataManager;
    private Subscription mSubscription;
    private MainActivityInterface view;

    public MainPresenter(DataManager dataManager) {
        mDataManager = dataManager;

    }

    //@Override
    public void attachView(MainActivityInterface mvpView) {
        super.attachView(mvpView);
        view = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null){
            mSubscription.unsubscribe();
            view =null;
        }
    }


    /**
     * 登陆
     */
    public void login() {
        Map<String, String> params = view.getLoginParams();
        mSubscription = mDataManager.login(params).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new Observer<User>() {
                    @Override
                    public void onCompleted() {
                        view.dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        try {
                            if (e != null&&view!=null) {
                                view.failed("网络请求失败，请检查您的网络 ");
                            }
                            view.dismissDialog();
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }

                    @Override
                    public void onNext(User user) {
                        view.dismissDialog();
                        if (user.getStatus() == Constant.SUCCESS) {
                            view.success(user);
                        } else {
                            view.failed(user.getMsg() + "");

                        }

                    }
                });
    }

    /**
     * 保存UserID;
     *
     * @param appContext
     * @param user
     */
    public void savaUser(AppContext appContext, User user) {
        ACache.get(appContext).put("User", user);
        appContext.setUser(user);
    }

    /**
     * 判断是否自动登陆和退出登陆及修改密码
     *
     * @param isLogout 退出登录标记 true登陆 退出、修改密码false
     */
    public void isLogout(boolean isLogout) {
        checkViewAttached();
        if (!isLogout) {
            //退出登陆、修改密码走输入密码操作
            view.exitLogin();
        } else {
            //自动登陆
//            mSubscription = Observable.timer(0, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribeOn(Schedulers.io())
//                    .subscribe(new Action1<Long>() {
//                        @Override
//                        public void call(Long s) {
                            view.autoLogin();
//                        }
//                    });
        }
    }
}
